/*
 * Example program for enriginenc plugin
 * 测试直接基于FFmpeg的编码器质量
 */

#include <gst/gst.h>
#include <iostream>

int main(int argc, char *argv[]) {
    GstElement *pipeline;
    GstBus *bus;
    GstMessage *msg;
    
    gst_init(&argc, &argv);
    
    std::cout << "=== Enrigin Encoder Test (Direct FFmpeg) ===" << std::endl;
    std::cout << "Testing encoding quality with different configurations" << std::endl;
    
    // Test 1: Default settings (VBR, high profile)
    std::cout << "\n[Test 1] Default VBR encoding..." << std::endl;
    pipeline = gst_parse_launch(
        "videotestsrc num-buffers=300 ! "
        "video/x-raw,format=NV12,width=1920,height=1080,framerate=30/1 ! "
        "enriginenc codec=h264 rc-mode=vbr profile=high ! "
        "h264parse ! mp4mux ! filesink location=test_enrigin_vbr.mp4",
        NULL);
    
    gst_element_set_state(pipeline, GST_STATE_PLAYING);
    bus = gst_element_get_bus(pipeline);
    msg = gst_bus_timed_pop_filtered(bus, GST_CLOCK_TIME_NONE,
        (GstMessageType)(GST_MESSAGE_ERROR | GST_MESSAGE_EOS));
    
    if (msg != NULL) gst_message_unref(msg);
    gst_object_unref(bus);
    gst_element_set_state(pipeline, GST_STATE_NULL);
    gst_object_unref(pipeline);
    std::cout << "✓ VBR test completed: test_enrigin_vbr.mp4" << std::endl;
    
    // Test 2: CBR encoding with 4Mbps
    std::cout << "\n[Test 2] CBR 4Mbps encoding..." << std::endl;
    pipeline = gst_parse_launch(
        "videotestsrc num-buffers=300 ! "
        "video/x-raw,format=NV12,width=1920,height=1080,framerate=30/1 ! "
        "enriginenc codec=h264 rc-mode=cbr bitrate=4000 profile=high ! "
        "h264parse ! mp4mux ! filesink location=test_enrigin_cbr_4m.mp4",
        NULL);
    
    gst_element_set_state(pipeline, GST_STATE_PLAYING);
    bus = gst_element_get_bus(pipeline);
    msg = gst_bus_timed_pop_filtered(bus, GST_CLOCK_TIME_NONE,
        (GstMessageType)(GST_MESSAGE_ERROR | GST_MESSAGE_EOS));
    
    if (msg != NULL) gst_message_unref(msg);
    gst_object_unref(bus);
    gst_element_set_state(pipeline, GST_STATE_NULL);
    gst_object_unref(pipeline);
    std::cout << "✓ CBR test completed: test_enrigin_cbr_4m.mp4" << std::endl;
    
    // Test 3: High bitrate for quality comparison
    std::cout << "\n[Test 3] High quality 8Mbps encoding..." << std::endl;
    pipeline = gst_parse_launch(
        "videotestsrc num-buffers=300 ! "
        "video/x-raw,format=NV12,width=1920,height=1080,framerate=30/1 ! "
        "enriginenc codec=h264 rc-mode=cbr bitrate=8000 profile=high ! "
        "h264parse ! mp4mux ! filesink location=test_enrigin_hq_8m.mp4",
        NULL);
    
    gst_element_set_state(pipeline, GST_STATE_PLAYING);
    bus = gst_element_get_bus(pipeline);
    msg = gst_bus_timed_pop_filtered(bus, GST_CLOCK_TIME_NONE,
        (GstMessageType)(GST_MESSAGE_ERROR | GST_MESSAGE_EOS));
    
    if (msg != NULL) gst_message_unref(msg);
    gst_object_unref(bus);
    gst_element_set_state(pipeline, GST_STATE_NULL);
    gst_object_unref(pipeline);
    std::cout << "✓ High quality test completed: test_enrigin_hq_8m.mp4" << std::endl;
    
    // Test 4: HEVC encoding
    std::cout << "\n[Test 4] HEVC encoding..." << std::endl;
    pipeline = gst_parse_launch(
        "videotestsrc num-buffers=300 ! "
        "video/x-raw,format=NV12,width=1920,height=1080,framerate=30/1 ! "
        "enriginenc codec=hevc rc-mode=cbr bitrate=4000 profile=main ! "
        "h265parse ! mp4mux ! filesink location=test_enrigin_hevc.mp4",
        NULL);
    
    gst_element_set_state(pipeline, GST_STATE_PLAYING);
    bus = gst_element_get_bus(pipeline);
    msg = gst_bus_timed_pop_filtered(bus, GST_CLOCK_TIME_NONE,
        (GstMessageType)(GST_MESSAGE_ERROR | GST_MESSAGE_EOS));
    
    if (msg != NULL) gst_message_unref(msg);
    gst_object_unref(bus);
    gst_element_set_state(pipeline, GST_STATE_NULL);
    gst_object_unref(pipeline);
    std::cout << "✓ HEVC test completed: test_enrigin_hevc.mp4" << std::endl;
    
    std::cout << "\n=== All tests completed ===" << std::endl;
    std::cout << "Compare quality with: ffplay test_enrigin_*.mp4" << std::endl;
    std::cout << "Check file sizes: ls -lh test_enrigin_*.mp4" << std::endl;
    
    return 0;
}
